

/**
 * 新增和修改表单提交验证
 *  使用注意 :
 *      表单form定义:id="form",提交按钮定义:id="submit",
 *      js定义变量：需要验证的字段：define_fileds，提交地址：ajaxurl
 */
$(function () {
    $('#form').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields:define_fileds
    });
    $(document).keyup(function (event) {
        if (event.keyCode == 13) {
            $("#submit").trigger("click");
        }
    });
    $("#form").submit(function (ev) {
        ev.preventDefault();
    });
    $("#submit").on("click", function () {
        var bootstrapValidator = $("#form").data('bootstrapValidator');
        bootstrapValidator.validate();
        if (bootstrapValidator.isValid()) {
            $.ajax({
                type: "POST",
                url: ajaxurl,
                data: $('#form').serialize(),
                beforeSend:function(){
                    $(this).attr("disabled","true"); //禁止点击
                },
                success: function (info) {
                    // console.log(info);
                    layer.msg(info.msg, {icon: info.code});
                    setTimeout(function () {
                        if(info.code == 1){
                            var index=parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            parent.history.go(0);
                        }else{
                            $('#submit').removeAttr("disabled");
                        }
                    }, 2000); //延迟演示
                },
                yes: function(index){
                    layer.close(loading);
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                    window.parent.location.reload();
                }
            });
            return false;
        } else return;
    });
});